package com.nce.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.nce.entity.Msg;

public class MsgParser {
	
	private String crtGroup;
	private Msg crtMsg;
	private String crtContent="";

	public static void main(String[] args) throws Exception {			
		
		MsgParser parser = new MsgParser();
		Msg msg = parser.isMsgHead("2012/7/29 16:57:21 lmm(2363311940)");
		System.out.println(msg);
	}

	public List<Msg> parseMsg(String path,Date sdate,Date edate) throws Exception {
		List<Msg> msgList = new ArrayList<Msg>();		
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				path), "UTF-8");


		BufferedReader br = new BufferedReader(isr);
		String lineStr = "";
		int lineNumber = 0;
		while ((lineStr = br.readLine()) != null) {		
//			if(lineStr.indexOf("2012/8/10 18:49:24 雀起(1553999548)")!=-1){
//				System.out.println(lineStr);
//			}
			lineNumber++;
			//lineStr = new String(lineStr.getBytes("GBK"),"UTF-8");			
			
			String groupTmp = isGroup(lineStr);
			if(groupTmp!=null){
				crtGroup = groupTmp;
			}
			
			if(crtGroup !=null){
				Msg msg = isMsgHead(lineStr);
				if(msg!=null){//如果是消息头, 设置新的消息为当前消息,并设置群组信息
					if(crtMsg!=null){//如果有当前消息,当前消息的内容设为当前内容.
						crtMsg.setContent(crtContent);
						crtMsg.setWordCounts(crtContent.length()-4);
						crtContent = "";
						crtMsg.setLineNumber(lineNumber);
						if(crtMsg.getCtime().after(sdate) && crtMsg.getCtime().before(edate)){
							msgList.add(crtMsg);
						}
						//return msgList;
						//System.out.println(crtMsg);
					}
					msg.setQqgroup(crtGroup);
					crtMsg = msg;
					
				}else{//不是消息头并且已经存在消息头了,为聊天内容
					if(crtMsg!=null){
						crtContent+=(lineStr+"\r\n");
					}
				}
			}
			

		}
		if(crtMsg!=null){
		crtMsg.setContent(crtContent);
		crtMsg.setWordCounts(crtContent.length()-4);
		crtMsg.setLineNumber(lineNumber);
		if(crtMsg.getCtime().after(sdate) && crtMsg.getCtime().before(edate)){
			msgList.add(crtMsg);
		}
		}
		//System.out.println(crtMsg);
		
		return msgList;
	}
	
	public String isGroup(String str){
		if(str==null||str.trim().equals("")){
			return null;
		}else{
			int idx = str.indexOf("消息对象:");
			if(idx !=-1){
				return str.substring(idx+5).trim();
			}else{
				return null;
			}
		}
	}
	
	public Msg isMsgHead(String str){
		Msg msg = new Msg();
		//2012/7/25 10:53:19 深圳D-樂天(7873972)
		String [] strs = str.split(" ");
		if(strs.length>=3){//按空格分三段
			String dateTimeStr = strs[0]+" "+strs[1];
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        try {
				Date date = (Date)formatter.parse(dateTimeStr);
				msg.setCtime(date);
			} catch (ParseException e) {
				//前两段不是日期就不是消息头
				return null;
			}
			//后面没有()中带数字就不是消息头
			String lastStr = "";
			for(int i = 2; i < strs.length; i++){
				lastStr += strs[i];
			}
			if(lastStr.lastIndexOf("(")!=-1 && lastStr.lastIndexOf(")")!= -1){
				String numStr = lastStr.substring(lastStr.lastIndexOf("(")+1,lastStr.lastIndexOf(")"));
				try{
					Long.parseLong(numStr);
				}catch(Exception e){
					//括号中不是数字就不是QQ号，那就不是消息头
					return null;
				}
				//System.out.println(str);
				msg.setQqid(numStr);
				msg.setUsername(lastStr.substring(0,lastStr.lastIndexOf("(")));
				return msg;
				//邮箱格式消息头 
				//2012/5/11 17:11:54 pmcaff+@牙疼毅语<wandm@winroadchina.com>
			}else if(lastStr.lastIndexOf("<")!=-1 && lastStr.lastIndexOf(">")!= -1){
				String emailStr = lastStr.substring(lastStr.lastIndexOf("<")+1,lastStr.lastIndexOf(">"));
				//不是邮箱
				if(emailStr.indexOf("@")==-1){
					return null;
				}
				//System.out.println(str);
				msg.setQqid(emailStr);
				msg.setUsername(lastStr.substring(0,lastStr.lastIndexOf("<")));
				return msg;
			}else{
				
				return null;
			}
		}else{
			return null;
		}
		
	}


}
